// pages/setting/setting.js
const app = getApp()
const util = require('../../utils/util.js')
var BLELocker = require("../../utils/BLELocker.js");
var blueConStatHandler = require("../../utils/blueConStatHandler.js");
// var openLockUtil = require("openLock.js")
const lbbService = require('../service/service.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    hospitalId: '',
    activeTab: '0',
    sysProblemList: [],
    serProblemList: [],
    setInter:'',
    scanning: false,
    timer:{},
    vueShow:true,// 
    equId:'',
    show: false, //控制下拉列表的显示隐藏，false隐藏、true显示
    selectData: ['6', '8', '10', '15', '20'], //下拉列表的数据
    index: 0 //选择的下拉列表下标
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
    // 点击下拉显示框
    selectTap() {
      this.setData({
        show: !this.data.show
      });
    },
    // 点击下拉列表
    optionTap(e) {
      clearInterval(this.data.setInter)
      let Index = e.currentTarget.dataset.index; //获取点击的下拉列表的下标
      this.setData({
        index: Index,
        show: !this.data.show
      });
      this.intetval()
      this.Gain()
    },
  searchRfid(){
    let that = this;
    that.scan(that.connectBox);
  },

//发送95
  connectBox() {
    let that = this;
    if (that.data.data_95) {
      if(that.data.data_95.rfid == 'FFFFFFFF'){
        that.setData({
          equipment:''
        })
        wx.showToast({
          title: '请插入设备！',
        })
      }else{
        that.getEquNum();
      }
      console.log(that.data.data_95,'95打印')
      that.setData({
        vueShow:false,
        data_95:that.data.data_95
      })

    } else {
      wx.showToast({
        title: '连接失败请重试',
        image: '/images/gantan.png'
      })
    }
  },
  intetval(){
    var that = this;
    that.Gain()
    that.data.setInter = setInterval(function(){
      BLELocker.setParams(function (status, data) {
        blueConStatHandler.handleStatus(status, data, that, that.connectBox, true)
      }, that);
      BLELocker.send95Command(null)
    },that.data.selectData[that.data.index]*1000)
  },
  send95Cmd:function(){
    var that = this;
    clearInterval(that.data.setInter)
    BLELocker.setParams(function (status, data) {
      blueConStatHandler.handleStatus(status, data, that, that.connectBox, true)
    }, that);
    BLELocker.send95Command(null)
  },
  getEquNum:function(){
    var that = this 
    wx.request({
      url: util.CONST_DATA.BASE_URL + '/rfidWheelchairCheck/getEquipmentInfo',
      method: "POST",
      header: app.globalData.header,
      data:{
        mac:that.data.macId,
        rfid:that.data.data_95.rfid
      },
      success:(res)=>{
        console.log(res.data.data.equipmentId,'后端返回请求数据')
        that.setData({
          equipment:res.data.data.equipmentId,
        })
        that.Gain()
      },
      fail:(err)=>{
        console.log(err,'后端返回请求数据')
        that.Gain()
      }
    })
  },
  /*扫码并连接*/
  scan: function(_callback){
    wx.showToast({
      title: '正在加载,请等待',
      icon:'loading'
    })
    var that = this;
    that.data.scanning = true;
    wx.scanCode({
      success(res) {
        console.log(res,'wangling')
        var qrc = '';
        console.log(res.result.indexOf(util.CONST_DATA.DEVICE_QRCODE_URL_PREFIX),'res.result.indexOf(util.CONST_DATA.DEVICE_QRCODE_URL_PREFIX)')
        if (res.result.indexOf(util.CONST_DATA.DEVICE_QRCODE_URL_PREFIX) == 0) {
          var rst = res.result.split('/');
          qrc = rst[rst.length - 1];
          that.data.scan_qrc = qrc;
          console.log(qrc,'wangling')
        }else{
          var rst = res.result.split(':');
          console.log(rst,'szy')
          for(let i= 0;i<rst.length;i++){
            qrc+= rst[i]
          }
          // qrc = rst[rst.length - 1];
          that.data.scan_qrc = qrc;
          console.log(qrc,'wangling')
        }
        that.data.scanning = false;
        console.log(that.data.scan_qrc,getApp().globalData.systemInfo ? getApp().globalData.systemInfo.system : 'ios')
        lbbService.getMacId({ 
          markCode: that.data.scan_qrc,
          system: getApp().globalData.systemInfo ? getApp().globalData.systemInfo.system : 'ios'
        },function(res){
          if (res.macId) {
            that.data.newConFlg = res.newConFlg;
            that.data.curIsDirectCon = res.curIsDirectCon;
            that.data.pileCode = res.pileId;
            that.data.macId = res.macId;
            that.data.wxDeviceId = getApp().getDeviceId(res.macId);
            that.data.pileType = res.categoryId;
            that.getConnect(res.macId, _callback);
          } else {
            wx.showToast({
              title: '获取mac失败',
            })
          }
        })
      },
      fail(res) {
        that.data.scanning = false;
      }
    })
  },
  getConnect: function (macId, _callback) {
    let that = this;
    that.setData({
      loading_show:true,
      loading_desc:'设备连接中...'
    })
    if (that.data.newConFlg) {
      BLELocker.newConnect('DK' + macId, function (status, data) {
        blueConStatHandler.handleStatus(status, data, that, _callback, true)
      }, 0, false, null, null, that);
    } else {
      BLELocker.directConnect('DK' + macId, function (status, data) {
        blueConStatHandler.handleStatus(status, data, that, _callback, true)
      }, 0, false, null, null, that);
    };
  },
  /**
   * 重试蓝牙连接
   */
  retryConnect() {
    console.log('retryConnect');
    let that = this;
    that.hideModal1();
    that.setData({
      loading_show: true,
      loading_desc: "设备连接中"
    });
    that.getConnect(that.data.macId);
  },
  hideModal1() {
    this.setData({
      modal: {
        modal_show: false,
        confirm: false,
        retry95: false,
        retryConnect: false
      }
    })
  },
  onShow: function () {
    this.setData({
      vueShow:true
    })
    clearInterval(this.data.setInter)
  },
  // Gain:function(e){
  //   let that = this
  //   let countDownNum = that.data.selectData[that.data.index]
  //   that.setData({
  //     disabled:true,
  //     timer: setInterval(function () {//这里把setInterval赋值给变量名为timer的变量
  //       //每隔一秒countDownNum就减一，实现同步
  //       countDownNum--;
  //       //然后把countDownNum存进data，好让用户知道时间在倒计着
  //       that.setData({
  //         countDownNum: countDownNum+'s'
  //       })
  //       //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
  //       if (countDownNum == 0) {
  //         //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
  //         //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
  //         clearInterval(that.data.timer)
  //         wx.showToast({
  //           title: '正在加载',
  //           icon:'loading'
  //         })
  //         // countDownNum = that.data.selectData[that.data.index]
  //         //关闭定时器之后，可作其他处理codes go here
  //       }
  //     }, 1000)
  //   })
  // },
  submit(){
    var that = this
    lbbService.getBangdingRfid({ 
      rfid:that.data.data_95.rfid,
      equId:that.data.equId
      // rfid:'4018',
      // equId:'100002'
    },function(res){
      console.log(res,'res')
      if (res.result == 0) {
        wx.showToast({
          title: '成功',
        })
      } else {
        wx.showToast({
          title: '获取mac失败',
        })
      }
    })
  },
  getEquipment(){
    var that = this 
    wx.scanCode({
      success(res) {
        console.log(res.result.substring(1),'wangling')
        that.setData({
          equId:res.result.substring(1)
        })
      },
      fail(res) {
        that.data.scanning = false;
      }
    })
  },
  changeValue(e){
    var that = this
    console.log(e)
    clearInterval(that.data.timer)
    that.data.timer =setTimeout(()=>{
      that.setData({
        equId:e.detail.value
      })
    },300) 
    that.setData({

    })
  }
})